package com.yny.administrator.android1607_meishi.avtivity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;

import com.yny.administrator.android1607_meishi.Consts;
import com.yny.administrator.android1607_meishi.R;
import com.yny.administrator.android1607_meishi.adapter.MyVpAdapter;
import com.yny.administrator.android1607_meishi.fragment.ContentFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private ViewPager mVp;
    private DrawerLayout mDrawer;
    private Toolbar mTool;
    private TabLayout mTab;
    private String[] titles=new String[]{};
    private List<Fragment> fragments=new ArrayList<>();
    private MyVpAdapter adapter;
    private Button mRegieter;
    private Button mLogin;
    private Button btnCol;
    private Button btnDel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //去除标题
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        //获取控件
        initView();
        initData();
        //设置drawerlayout
        mTool.inflateMenu(R.menu.main);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, mDrawer, mTool, R.string.drawer_open, R.string.drawer_close);
        toggle.syncState();
        mDrawer.addDrawerListener(toggle);
        //设置注册，登陆监听
        mRegieter.setOnClickListener(this);
        mLogin.setOnClickListener(this);
        btnCol.setOnClickListener(this);
        btnDel.setOnClickListener(this);
        //设置viewpager适配器
        adapter = new MyVpAdapter(getSupportFragmentManager(), fragments, titles);
        //设置从适配器获取标题
        mTab.setTabsFromPagerAdapter(adapter);
        mVp.setAdapter(adapter);
        mVp.setOffscreenPageLimit(16);
        //要写在mvp设置适配器之后，源码
        mTab.setupWithViewPager(mVp);
    }

    /**
     * 初始化数据
     */
    private void initData() {
        titles = getResources().getStringArray(R.array.title);
        for (int i = 0; i < titles.length; i++) {
            ContentFragment fragment = new ContentFragment();
            Bundle args = new Bundle();
            args.putString("url",Consts.JSON_URL[i]);
            fragment.setArguments(args);
            fragments.add(fragment);
        }
    }

    /**
     * 获取控件
     */
    private void initView() {
        mVp = (ViewPager) findViewById(R.id.vp_contents);
        mDrawer = (DrawerLayout) findViewById(R.id.drawer);
        mTool = (Toolbar) findViewById(R.id.toolbar);
        mTab = (TabLayout) findViewById(R.id.tab_title);
        mRegieter = (Button) findViewById(R.id.register);
        mLogin = (Button) findViewById(R.id.login);
        btnCol = (Button) findViewById(R.id.collect);
        btnDel = (Button) findViewById(R.id.delete);
    }

    /**
     * @param view
     * 实现注册，登录监听
     */
    @Override
    public void onClick(View view) {
        switch(view.getId()){
            //实现注册
            case R.id.register :
                Intent intentR = new Intent(MainActivity.this, RegieterActivity.class);
                startActivity(intentR);
            break;
            //实现登录
            case R.id.login:
//                BmobUser currentUser = BmobUser.getCurrentUser(MainActivity.this, BmobUser.class);

//                if (null==currentUser){
                    Intent intentL = new Intent(MainActivity.this, LoginActivity.class);
                    startActivity(intentL);
//                }else {
//                    Toast.makeText(MainActivity.this, "您已经登陆，请不要重复登陆", Toast.LENGTH_SHORT).show();
//
//                }

            break;
            case R.id.collect:
                Intent intentC = new Intent(MainActivity.this, CollectActivity.class);
                startActivity(intentC);
                break;
            case R.id.delete:
                int i = ContentsActivity.mDb.delete("movie", null, null);
                if (i>=0){
                    Toast.makeText(MainActivity.this, "删除收藏成功", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

}
